package com.sanri.buildingblock.exportcenter.dao.typehandler;

import com.sanri.buildingblock.exportcenter.enums.OperationType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @description:
 * @author: huangzhengri
 * @time: 2023-04-14 15:37
 */
@MappedTypes(value = Integer.class)
@MappedJdbcTypes(value = {JdbcType.TINYINT, JdbcType.INTEGER})
public class OpTypeHandler implements TypeHandler<OperationType> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, OperationType operationType, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i,operationType.getCode());
    }

    @Override
    public OperationType getResult(ResultSet resultSet, String columnName) throws SQLException {
        int code = resultSet.getInt(columnName);
        return OperationType.getOperationTypeByCode(code);
    }

    @Override
    public OperationType getResult(ResultSet resultSet, int columnIndex) throws SQLException {
        int code = resultSet.getInt(columnIndex);
        return OperationType.getOperationTypeByCode(code);
    }

    @Override
    public OperationType getResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        int code = callableStatement.getInt(columnIndex);
        return OperationType.getOperationTypeByCode(code);
    }
}
